package com.damage.common.init;

import com.damage.common.config.DepartmentConfig;
import com.damage.common.config.SupplierConfig;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class InitGroup {

    @Autowired
    private IdentityService identityService;

    @Autowired
    private DepartmentConfig departmentConfig;

    @Autowired
    private SupplierConfig supplierConfig;

    private static final int GROUP_SIZE_NONE = 0;
    private static final int GROUP_SIZE_NORMAL = 4;

    public void init() {
        //系统默认有的所有组名
        List<String> allGroupId = new ArrayList<String>();
        allGroupId.addAll(DepartmentConfig.deptMap.values());
        allGroupId.add(SupplierConfig.group);
        //activiti存在的所有组
        List<Group> groups = identityService.createGroupQuery().list();

        for (String groupId: allGroupId) {
            Boolean result = ifGroupExist(groupId, groups);
            if (result == false){
                //组不存在
                Group newGroup = identityService.newGroup(groupId);
                identityService.saveGroup(newGroup);
            }
        }
    }

    //判断组是否存在
    private Boolean ifGroupExist(String groupId,List<Group> groups){
        for (Group group : groups) {
            if (group.getId().equals(groupId)){
                return true;
            }
        }
        return false;
    }
}
